<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class page2_process extends Admin_Controller
{
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_list_model');
		$col = $CI->uri->segment($CI->page2_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->page2_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->page2_list_model->cache->add($k, $order[$CI->page2_list_model->cache->load($k)]);
			} else
				$CI->page2_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->page2_list_model->url_prefix.'/'.$CI->page2_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_list_model');
		$flag = 0;
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				if ($CI->page2_list_model->check_delete($id))
				{
					$flag = 1;
					$CI->db->delete(PAGE, array(PAGE_PAGE_ID=>$id, PAGE_FRANCHISE_ID=>$CI->page2_list_model->admin_id()));			
				}
			}
		}
		if ($flag)
			// message
			$CI->page2_list_model->cache->add($CI->page2_list_model->msg_cache_prefix, $CI->lang->line('list_delete_all'));
		else
			// message
			$CI->page2_list_model->cache->add($CI->page2_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete')." : ".$CI->block_list_model->error_string);
		
		// go to original page
		redirect($CI->page2_list_model->url_prefix.'/'.$CI->page2_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_list_model');
		
		$id = $CI->uri->segment($CI->page2_list_model->item_segment);
		if ($CI->page2_list_model->check_delete($id))
		{
			$CI->db->delete(PAGE, array(PAGE_PAGE_ID=>$id, PAGE_FRANCHISE_ID=>$CI->page2_list_model->admin_id()));
		
			// message
			$CI->page2_list_model->cache->add($CI->page2_list_model->msg_cache_prefix, $CI->lang->line('list_delete'));
		} else
			// message
			$CI->page2_list_model->cache->add($CI->page2_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete')." : ".$CI->block_list_model->error_string);
		
		// go to original page
		redirect($CI->page2_list_model->url_prefix.'/'.$CI->page2_list_model->start);
	}
	
	function addBlock()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_detail_model');
		
		$j = $CI->uri->segment(5);
		$limit = $CI->page2_detail_model->cache->load('limit'.$j);
		if (!$limit) $limit = 5;
		$CI->page2_detail_model->cache->add('limit'.$j, $limit+1);
		$CI->page2_detail_model->cache->add('post', $_POST);
		
		// go to original page
		redirect($CI->page2_detail_model->url_prefix.'/'.$CI->page2_detail_model->start.'/'.$CI->page2_detail_model->item);
	}
	
	function removeBlock()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_detail_model');
		
		$j = $CI->uri->segment(5);
		$limit = $CI->page2_detail_model->cache->load('limit'.$j);
		if (!$limit) $limit = 6;
		$CI->page2_detail_model->cache->add('limit'.$j, $limit-1);
		$CI->page2_detail_model->cache->add('post', $_POST);
		
		// go to original page
		redirect($CI->page2_detail_model->url_prefix.'/'.$CI->page2_detail_model->start.'/'.$CI->page2_detail_model->item);
	}	
	
	function saveItem()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('title', 'Title', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->page2_detail_model->cache->add($CI->page2_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->validation->error_string());
			$CI->page2_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->page2_detail_model->url_prefix.'/'.$CI->page2_detail_model->start.'/'.$CI->page2_detail_model->item);
		}
		
		// save?
		if (!$CI->page2_detail_model->check())
		{
			// message
			$CI->page2_detail_model->cache->add($CI->page2_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->lang->line('_name_duplicate'));
			$CI->page2_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->page2_detail_model->url_prefix.'/'.$CI->page2_detail_model->start.'/'.$CI->page2_detail_model->item);
		}
		
		if ($CI->page2_detail_model->hasPage2())
		{
			// update
			$sql = "UPDATE ".PAGE." SET";
			$sql .= " ".PAGE_TITLE."=".$CI->db->escape($CI->input->post('title'));
			$sql .= ", ".PAGE_CSS_FILE."=".$CI->db->escape($CI->input->post('css_file'));
			$sql .= " WHERE ".PAGE_FRANCHISE_ID."=".$CI->page2_detail_model->admin_id();
			$sql .= " AND ".PAGE_PAGE_ID."=".$CI->page2_detail_model->item;
			$CI->db->query($sql);
		} else {
			$CI->page2_detail_model->item = $CI->page2_detail_model->maxkey();

			// insert
			$data = array(
				PAGE_TITLE=>$CI->input->post('title'),
				PAGE_CSS_FILE=>$CI->input->post('css_file'),
				PAGE_IFRAME=>1,
				PAGE_FRANCHISE_ID=>$CI->page2_detail_model->admin_id(),
				PAGE_PAGE_ID=>$CI->page2_detail_model->item,
			);
			$CI->db->insert(PAGE, $data);
		}
		
		// save cell_link
		$cols = array();
		foreach($_POST as $k=>$v)
		{
			if (ereg('col_', $k))
			{
				list($x, $j, $i) = explode('_', $k);
				if ($v)
				{
					$cols[] = array($j, $i, $v);
				}
			}
		}

		// insert?
		foreach ($cols as $a)
		{
			list($j, $i, $v) = $a;
			
			foreach ($cols as $b)
			{
				if ($a !== $b && $b[0] == $j && $b[2] == $v)
				{
					// message
					$CI->page2_detail_model->cache->add($CI->page2_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : There're 2 same blocks in column {$j}. Please enter other");
					$CI->page2_detail_model->cache->add('post', $_POST);
		
					// go to original page
					redirect($CI->page2_detail_model->url_prefix.'/'.$CI->page2_detail_model->start.'/'.$CI->page2_detail_model->item);
				}
			}
		}
		
		// delete old cell_link
		$sql = "DELETE FROM ".CELL_LINK." WHERE ".CELL_LINK_FRANCHISE_ID."=".$CI->page2_detail_model->admin_id()." AND ".CELL_LINK_PAGE_ID."=".$CI->page2_detail_model->item;
		$CI->db->query($sql);
		
		foreach ($cols as $a)
		{
			list($j, $i, $v) = $a;
			
			// insert
			$data = array(
				CELL_LINK_FRANCHISE_ID=>$CI->page2_detail_model->admin_id(),
				CELL_LINK_PAGE_ID=>$CI->page2_detail_model->item,
				CELL_LINK_COL_ID=>$j,
				CELL_LINK_BLOCK_ID=>$v,
				CELL_LINK_ORDER_ID=>$i,
			);
			
			$CI->db->insert(CELL_LINK, $data);
		}			

		// message
		$CI->page2_detail_model->cache->add($CI->page2_detail_model->msg_cache_prefix, $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->page2_detail_model->url_prefix2.'/'.$CI->page2_detail_model->start);
	}		
}

?>